<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head>
  <meta charset="utf-8">
  <title>用户管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" th:href="@{/layui/css/layui.css}" media="all">
  <link rel="stylesheet" th:href="@{/adminui/dist/css/admin.css}" media="all">
  <link rel="stylesheet" th:href="@{/style/custom.css}" media="all">
  <style>
    /*.layui-layer-btn .layui-layer-btn0 {*/
    /*    border: 1px solid #dedede;*/
    /*    background-color: #fff;*/
    /*    color: #333;*/
    /*}*/
    /*.layui-layer-btn .layui-layer-btn1 {*/
    /*    border-color: #1e9fff;*/
    /*    background-color: #1e9fff;*/
    /*    color: #fff*/
    /*}*/
    /*.layui-layer-btn .layui-layer-btn3 {*/
    /*    border: 1px solid #dedede;*/
    /*    background-color: #fff;*/
    /*    color: #333;*/
    /*}*/
  </style>
</head>
<body>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto layui-form-pane" lay-filter="LAY-user-front-form">
      <div class="layui-form-item">
        <div class="layui-inline">
          <label class="layui-form-label">用户ID</label>
          <div class="layui-input-block">
            <input type="text" name="userId" placeholder="请输入" autocomplete="off" class="layui-input" lay-affix="clear">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">用户名</label>
          <div class="layui-input-block">
            <input type="text" name="username" placeholder="请输入" autocomplete="off" class="layui-input" lay-affix="clear">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">所属组织</label>
          <div class="layui-input-block" id="orgId" style="width: 186px;margin-top: 1px;">
          </div>
        </div>
        <div class="layui-inline">
          <label class="layui-form-label">状态</label>
          <div class="layui-input-block">
            <select name="status" id="status" lay-search>
              <option value="">全部</option>
              <option th:each="map,idx:${statusList}" th:value="${map.key}" th:text="${map.value}"></option>
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="LAY-user-front-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i> 搜索
          </button>
          <button class="layui-btn layui-btn-primary layui-btn-sm" lay-submit lay-filter="LAY-user-front-reset">
            <i class="layui-icon layui-icon-refresh layuiadmin-button-btn"></i> 重置
          </button>
        </div>
      </div>
    </div>

    <div class="layui-card-body">
      <div class="layui-btn-group" style="padding-bottom: 10px;">
        <!--刷新table-->
        <button type="button" class="layui-btn layui-btn-normal btn-op-user layui-btn-sm" data-type="refresh">
          <i class="layui-icon">&#xe666;</i>刷新
        </button>
        <!-- 新增 -->
        <button sec:authorize="hasAuthority('sys:user:add')" type="button" class="layui-btn layui-btn-normal btn-op-user layui-btn-sm" data-type="add">
          <i class="layui-icon">&#xe654;</i>新增
        </button>
      </div>

      <!--table 定义-->
      <table id="LAY-user-table" lay-filter="LAY-user-table"></table>
      <!--table 列转换-->
      <script type="text/html" id="imgTpl">
        <img style="display: inline-block; width: 50%; height: 100%;" src={{ d.avatar }}>
      </script>
      <script type="text/html" id="statusSwitchTpl">
        <input type="checkbox" disabled name="status" value="{{d.status}}" lay-skin="switch" lay-text="启用|禁用" {{ d.status== 'Y' ?'checked' : '' }}>
      </script>

      <!--列表操作项-->
      <script type="text/html" id="user-table">
        <a sec:authorize="hasAuthority('sys:user:edit')" class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
<!--        <a sec:authorize="hasAuthority('sys:user:del')" class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>-->
        <a class="layui-btn layui-btn-xs" lay-event="more">更多 <i class="layui-icon layui-icon-down"></i></a>
      </script>
    </div>
  </div>
</div>

<script th:src="@{/layui/layui.js}"></script>
<script>
  layui.config({
    base: '/'
  }).extend({
    //设定模块别名，模块名是使用layui.模块名  模块文件是 js名字。不设置模块名 = 模块文件名
    userInfo: 'modules/sys/userInfo',
    xmSelect: 'extend/xm-select/dist/xm-select'
  }).use(['userInfo', 'xmSelect'], function(){
    let admin = layui.admin;
    let form = layui.form;
    let table = layui.table;
    let xmSelect = layui.xmSelect;
    let dropdown = layui.dropdown;

    //所属组织
    let orgId_select = xmSelect.render({
      el: '#orgId',
      name: 'orgId',
      // layVerify: 'required',
      layVerType: 'msg',
      //显示为text模式
      model: {label: {type: 'text'}},
      //单选模式
      radio: true,
      //选中关闭
      clickClose: true,
      toolbar: {
        show: true,
        list: ['CLEAR']
      },
      filterable: true,
      //树
      tree: {
        show: true,
        //非严格模式
        strict: false,
        //展开所有节点
        expandedKeys: true,
      },
      data: []
    });

    //加载组织机构下拉选
    admin.req({
      url: '/sys/organization/getSelectList',
      type: 'post',
      done: function (res) {
        orgId_select.update({
          data: res.data,
          autoRow: true,
        });
        //异步远程加载数据，需要重新设置
        orgId_select.changeExpandedKeys(true);
      }
    });


    //监听搜索
    form.on('submit(LAY-user-front-search)', function (data) {
      let param = data.field;
      //执行重载
      table.reload('LAY-user-table', {
        where: {param}
      });
    });
    // 监听重置
    form.on('submit(LAY-user-front-reset)', function (data) {
      let field = data.field;
      for (let key of Object.keys(field)) {
        field[key] = '';
      }
      form.val("LAY-user-front-form", field);

      orgId_select.setValue([]);
    });


    //事件
    let active = {
      add: function () {
        layer.open({
          type: 2
          , title: '新增'
          , content: '/sys/user/turn/edit'
          , area: ['100%', '100%']
          , resize: false
          , btn: ['确认', '重置', '取消']
          , btnAlign: 'r'
          , maxmin: true
          , yes: function (index, layero) {
            let iframeWindow = window['layui-layer-iframe' + index]
                , submitID = 'LAY-user-front-submit'
                , submit = layero.find('iframe').contents().find('#' + submitID);
            submit.trigger('click');
          }
          , btn2: function (index, layero) {
            let reset = layero.find('iframe').contents().find('#LAY-user-front-reset');
            reset.trigger('click');
            return false; // 阻止关闭
          }
          , btn3: function (index, layero) {
            layer.close(index);
            //数据刷新
            // table.reload('LAY-user-table');
          }
          , end: function () {
            //数据刷新
            table.reload('LAY-user-table');
          }
        });
      }
      , refresh: function () {
        let param = form.val("LAY-user-front-form");
        //执行重载 数据刷新
        table.reload('LAY-user-table', {
          where: {param}
        });
      }
    };

    //执行事件操作
    layui.$('.layui-btn.btn-op-user').on('click', function () {
      let type = layui.$(this).data('type');
      active[type] ? active[type].call(this) : '';
    });

    //监听工具条
    table.on('tool(LAY-user-table)', function (obj) {
      let line_data = obj.data;
      if (obj.event === 'more') {
        let more = [];
        if([[${#authorization.expression('hasAuthority("sys:user:password")')}]]) {
          more.push({
            title: '密码',
            id: 'password',
            templet: '<i class="layui-icon layui-icon-edit"></i> 重置密码',
            href: '#'
          })
        }
        if([[${#authorization.expression('hasAuthority("sys:user:del")')}]]) {
          more.push({
            title: "删除",
            id: "del",
            templet: '<i class="layui-icon layui-icon-delete"></i> 删除',
            href: '#'
          })
        }

        //下拉菜单
        dropdown.render({
          elem: this, // 触发事件的 DOM 对象
          show: true, // 外部事件触发即显示
          align: 'left ' , // 右对齐弹出
          data: more,
          click: function (mdata) {
            if (mdata.id === "password") {
              layer.open({
                type: 1,
                area: '350px',
                title: '重置密码',
                resize: false,
                btn: ['确定', '取消'],
                content: `
                <div class="layui-form" lay-filter="filter-password-layer" style="margin: 16px;">
                  <input type="hidden" id="id" name="id" value="`+ line_data.id +`"/>
                  <input type="hidden" id="userId" name="userId" value="`+ line_data.userId +`"/>
                  <div class="demo-login-container">
                    <div class="layui-form-item">
                      <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                          <i class="layui-icon layui-icon-password"></i>
                        </div>
                        <input type="password" name="password" value="`+ line_data.password +`" lay-verify="required" placeholder="密码" lay-reqtext="请填写密码" autocomplete="off" class="layui-input" lay-affix="clear">
                      </div>
                    </div>
                    <div class="layui-form-item">
                      <div class="layui-input-wrap">
                        <div class="layui-input-prefix">
                          <i class="layui-icon layui-icon-vercode"></i>
                        </div>
                        <input type="password" name="confirmPassword" value="" lay-verify="required" placeholder="确认密码" lay-reqtext="请填写确认密码" autocomplete="off" class="layui-input" lay-affix="clear">
                      </div>
                    </div>
                    <div class="layui-form-item layui-hide">
                      <button class="layui-btn layui-btn-fluid layui-bg-blue" lay-submit lay-filter="password-reset" id="password-reset">确认</button>
                    </div>
                  </div>
                </div>
              `,
                success: function(layero){
                  // 对弹层中的表单进行初始化渲染
                  form.render();
                },
                yes: function (index, layero) {
                  let submit = layero.find('#password-reset');
                  // 表单提交事件
                  form.on('submit(password-reset)', function(data){
                    let field = data.field; // 获取表单字段值
                    if (field.confirmPassword !== field.password) {
                      //执行成功
                      layer.msg("两次输入的密码不一致！", {icon: 2});
                      return ;
                    }
                    delete field.confirmPassword;

                    //提交 Ajax 成功后，更新表格中的数据
                    admin.req({
                      url: '/sys/user/set/password'
                      , type: 'post'
                      , contentType: 'application/json;charset=utf-8'
                      , data: JSON.stringify(field)
                      , done: function (res) {
                        //执行成功
                        layer.msg(res.msg, {icon: 1});
                        table.reload('LAY-user-table'); //数据刷新
                        layer.close(index); //关闭弹层
                      }
                    });
                    // return false; // 阻止默认 form 跳转
                  });
                  submit.trigger('click');
                }
              });
            } else if (mdata.id === "del") {
              if ('10000' == line_data.userId) {
                layer.msg('Admin用户不允许删除！', {icon: 4, time: 1000, shade: [0.3, '#000', true]});
                return;
              }

              layer.confirm('真的删除行么？', function (index) {
                //提交 Ajax 成功后，更新表格中的数据
                admin.req({
                  url: '/system/user/deleteById/' + line_data.id
                  , type: 'get'
                  , done: function (res) {
                    //执行成功
                    layer.msg(res.msg, {icon: 1});
                    table.reload('LAY-user-table'); //数据刷新
                    layer.close(index); //关闭弹层
                  }
                });
              });
            }
          }
        });
      } else if (obj.event === 'edit') {
        // let tr = $(obj.tr);
        layer.open({
          type: 2
          , title: '修改'
          , content: '/sys/user/turn/edit?id=' + line_data.id
          , area: ['100%', '100%']
          , resize: false
          , btn: ['确认', '重置', '取消']
          , btnAlign: 'r'
          , maxmin: true
          , yes: function (index, layero) {
            let iframeWindow = window['layui-layer-iframe' + index]
                , submitID = 'LAY-user-front-submit'
                , submit = layero.find('iframe').contents().find('#' + submitID);
            submit.trigger('click');
          }
          , btn2: function (index, layero) {
            let reset = layero.find('iframe').contents().find('#LAY-user-front-reset');
            reset.trigger('click');
            return false; // 阻止关闭
          }
          , btn3: function (index, layero) {
            layer.close(index);
            //数据刷新
            // table.reload('LAY-user-table');
          }
          , end: function () {
            //数据刷新
            table.reload('LAY-user-table');
          }
        });
      }
    });

  });
</script>
</body>
</html>
